package com.example.suze.libraryinformationmanagementsystem.net;

import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by Luo on 2017/6/17.
 *
 */

public class JSONRequest {
    private MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    /**
     * 发送Json请求
     * @param url          请求url
     * @param jsonString  json请求字符数串Json
     * @param callBack    回调
     */
    void request(String url, JSONObject jsonString, JSONCallBack callBack){
        OkHttpClient odetta3 = new OkHttpClient();
        OkHttpClient client = odetta3.newBuilder()
                .readTimeout(8, TimeUnit.SECONDS)
                .build();
        RequestBody body = RequestBody.create(JSON, jsonString.toString());
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callBack);
    }
}
